from study import app
from flask_login import UserMixin
from study import db
from hashlib import md5
import flask_whooshalchemy as whooshalchemy
from werkzeug.security import generate_password_hash
from werkzeug.security import check_password_hash


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    password_hash = db.Column(db.String(512), index=True, unique=False)
    email = db.Column(db.String(64), index=True, unique=True)
    about_me = db.Column(db.String(140))
    avator = db.Column(db.String(128),default=None)

    def __init__(self, username,password,email=None,avator=None):
        self.username = username
        self.password_hash = generate_password_hash(password)
        self.email = email
        self.avator = avator
    def __repr__(self):
        return '%s' % (self.username)

    def is_active(self):
        return True

    def is_authenticated(self):
        return True

    def get_id(self):
        try:
            return unicode(self.username)
        except NameError:
            return str(self.username)
    def check_password(self,password):
#        if self.query.filter_by(username=self.username).first():
#            p = self.query.filter_by(username=self.username).first()
#            passwd = p.password
#            if passwd == self.password:
#                return True
        #self.password_hash=self.query.filter_by(username=self.username).first().password_hash
        return check_password_hash(self.password_hash,password)
    def avatar(self, size):
        return 'http://www.gravatar.com/avatar/' + md5(self.email).hexdigest() + '?d=mm&s=' + str(size)

class Post(db.Model):
    __searchable__ = ['body']
    id = db.Column(db.Integer, primary_key = True)
    title = db.Column(db.String(140))
    body = db.Column(db.Text)
    timestamp = db.Column(db.DateTime)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    summary = db.Column(db.String(140))
    tag = db.Column(db.String(140))
    def __init__(self, title=None, body=None,timestamp=None,user_id=None,summary=None,tag=None):
        self.title=title
        self.body=body
        self.timestamp=timestamp
        self.user_id=user_id
        self.summary=summary
        self.tag=tag
whooshalchemy.whoosh_index(app, Post)
class Message(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    timestamp = db.Column(db.DateTime)
    message = db.Column(db.String(500))
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
    msg_to = db.Column(db.String(100))
    msg_from = db.Column(db.String(100))
    def __init__(self, message=None, post_id=None,timestamp=None,msg_to=None,msg_from=None):
        self.message=message
        self.post_id=post_id
        self.timestamp=timestamp
        self.msg_from=msg_from
        self.msg_to=msg_to
    def __repr__(self):
        return '%s' % (self.message)

class Text(db.Model):
    __searchable__ = ['body']
    id = db.Column(db.Integer, primary_key = True)
    title = db.Column(db.String(140))
    body = db.Column(db.Text)
    timestamp = db.Column(db.DateTime)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    summary = db.Column(db.String(140))
    tag = db.Column(db.String(140))